from datetime import datetime

from dateutil import tz

from elementary.utils.time import (
    convert_time_to_timezone,
    datetime_strftime,
    get_formatted_timedelta,
)


def test_datetime_strftime_without_timezone():
    str_datetime = datetime_strftime(
        datetime(2010, 1, 1, 1, 1, 1, 1), include_timezone=False
    )
    assert str_datetime == "2010-01-01 01:01:01"


def test_datetime_strftime_with_timezone():
    str_datetime = datetime_strftime(
        datetime(2010, 1, 1, 1, 1, 1, 1, tzinfo=tz.tzutc()), include_timezone=True
    )
    assert str_datetime == "2010-01-01 01:01:01 UTC"


def test_get_formatted_timedelta_days_delta():
    formatted_timedelta = get_formatted_timedelta(173000)
    assert formatted_timedelta == "2 days 0h 3m 20s"


def test_get_formatted_timedelta_hours_delta():
    formatted_timedelta = get_formatted_timedelta(43200)
    assert formatted_timedelta == "12 hours 0m 0s"


def test_get_formatted_timedelta_minutes_delta():
    formatted_timedelta = get_formatted_timedelta(3001)
    assert formatted_timedelta == "50 minutes 1s"


def test_get_formatted_timedelta_seconds_delta():
    formatted_timedelta = get_formatted_timedelta(12)
    assert formatted_timedelta == "12 seconds"


def test_convert_time_to_timezone():
    # test time with no timezone is handled by default as utc
    date = datetime(2010, 1, 1, 1, 1, 1, 1)
    date_with_timezone = convert_time_to_timezone(date)
    assert date.tzname() is None
    assert date_with_timezone.tzname() == "UTC"

    # test default timezone to convert to is utc by default
    date = datetime.fromisoformat("2010-01-01T01:01:01+01:00")
    date_with_timezone = convert_time_to_timezone(date)
    assert date.tzname() == "UTC+01:00"
    assert date_with_timezone.tzname() == "UTC"
    assert date.hour == 1
    assert date_with_timezone.hour == 0
    assert (date - date_with_timezone).total_seconds() == 0

    date = datetime.fromisoformat("2010-01-01T01:01:01+01:00")
    utc_plus_2_timezone = "Etc/GMT-2"
    date_with_timezone = convert_time_to_timezone(date, utc_plus_2_timezone)
    assert date.tzname() == "UTC+01:00"
    assert date_with_timezone.tzname() == "+02"
    assert date.hour == 1
    assert date_with_timezone.hour == 2
    assert (date - date_with_timezone).total_seconds() == 0
